package org.bees.cms.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public abstract class Files {
	private static String root;

	public static void config(Properties properties) {
		root = properties.getProperty("context.root");
	}

	public static String relate(String absolute) {
		String temp = absolute.startsWith(root) ? absolute.replace(root, "") : absolute;
		return temp.replace("\\", "/");
	}

	public static String absolute(String relate) {
		String temp = relate.startsWith(root) ? relate : root + relate;
		return temp.replace("/", "\\");
	}

	public static void copy(File source, File destination) throws Exception {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(source);
			os = new FileOutputStream(destination);
			byte[] buffer = new byte[1024];
			int length = 0;
			while ((length = is.read(buffer)) != -1) {
				os.write(buffer, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				os.close();
			}
			if (is != null) {
				is.close();
			}
		}
	}

	public static void destroy() {
		root = null;
	}

}
